use super::COB_INDEX_SIZE;

pub struct COBTranslator;

impl COBTranslator {
    pub fn track_index_to_cob_index(index: usize) -> usize {
        assert!(index < COB_INDEX_SIZE);
        if index < 64 {
            let groups_index = index % 8;
            let group_index = index / 8;
            8 * groups_index + group_index
        } else {
            let index = index - 64;
            let groups_index = index % 8;
            let group_index = index / 8;
            8 * groups_index + group_index + 64
        }
    }

    pub fn cob_index_to_track_index(index: usize) -> usize {
        assert!(index < COB_INDEX_SIZE);
        if index < 64 {
            let groups_index = index % 8;
            let group_index = index / 8;
            8 * groups_index + group_index
        } else {
            let index = index - 64;
            let groups_index = index % 8;
            let group_index = index / 8;
            8 * groups_index + group_index + 64
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_track_index_to_cob_index() {
        assert_eq!(COBTranslator::track_index_to_cob_index(0), 0);
        assert_eq!(COBTranslator::track_index_to_cob_index(1), 8);
        assert_eq!(COBTranslator::track_index_to_cob_index(127), 127);
        assert_eq!(COBTranslator::track_index_to_cob_index(126), 119);
        assert_eq!(COBTranslator::track_index_to_cob_index(64), 64);
        assert_eq!(COBTranslator::track_index_to_cob_index(72), 65);

        assert_eq!(COBTranslator::track_index_to_cob_index(121), 79);

        assert_eq!(COBTranslator::track_index_to_cob_index(126), 119);

        assert_eq!(COBTranslator::track_index_to_cob_index(55), 62);

        assert_eq!(COBTranslator::track_index_to_cob_index(65), 72);

        assert_eq!(COBTranslator::track_index_to_cob_index(83), 90);
    }

    #[test]
    fn test_cob_index_to_track_index() {
        assert_eq!(COBTranslator::cob_index_to_track_index(0), 0);
        assert_eq!(COBTranslator::cob_index_to_track_index(1), 8);
        assert_eq!(COBTranslator::cob_index_to_track_index(127), 127);
        assert_eq!(COBTranslator::cob_index_to_track_index(126), 119);
        assert_eq!(COBTranslator::cob_index_to_track_index(64), 64);
        assert_eq!(COBTranslator::cob_index_to_track_index(72), 65);

        assert_eq!(COBTranslator::cob_index_to_track_index(121), 79);

        assert_eq!(COBTranslator::cob_index_to_track_index(126), 119);

        assert_eq!(COBTranslator::cob_index_to_track_index(55), 62);

        assert_eq!(COBTranslator::cob_index_to_track_index(65), 72);

        assert_eq!(COBTranslator::cob_index_to_track_index(83), 90);
    }
}
